Scala zipwithindex

zipwithindex in scala is used to bind the element with some counter. As the name suggests ‘zipwithindex’ is used when we want to have index for our list of the element in scala. In short, we can say zipwithindex is used to bind all the elements of the list present inside with some particular index. zipwithindex always returns a new collection in scala, also zipwithindex can be used with both types of list i.e. immutable and mutable collections. zipwithindex method always returns a tuple2 collection like an array in scala. In the coming section, we will see how this method can be used in programming. zipwithindex method can be directly used on the immutable and immutable collection in scala and this method will give us a new tuple always with all the elements of the collection is bind with index.

Let’s see the syntax for zipwithindex method how we can use this in programming see below;

1) In this, we are calling foreach method with zipWithIndex, but we have mentioned the collection as well at the starting.

collection.zipWithIndex.foreach {

// our logic goes here for program.

}

2) In this syntax we are also creating a zipWithIndex by calling the zipWithIndex on the collection.

val myzicollection: Seq[(String, Int)] = collection_name.zipWithIndex

In the coming section, we will see a practice example of how we can use this in our programming to maintain the index in a collection data structure in Scala.

How zipwithindex Method works in Scala?

As now we know that zipwithindex method is used to deal with the indexing of the list element. This method helps us to create and generate the index of the mutable and immutable collection in Scala. With the help of these methods each and, every element present inside our collection got associated with an index. This is can also access the element by using their index. One more important thing about the zipwithindex method is that it always returns a new collection in Scala without being modified the existing collection on which we are calling it.

As we all know by the use of indexing searching and accessing of elements became easy and hence increase the performance of the program as well. Now we will discuss the complete signature of this method in detail how it looks and what does it return. Let’s see below for better understanding see below;

val mylist = List("hello", "bye", "catch", "dog", "ele", "fan", "pink")

val zipWithIndexMyList = mylist.zipWithIndex


This is the basic example to use zipWithIndex in our program to create an index for our collection. Here we are creating a list that contains some elements inside it. We can assign any number of elements inside the list. After this, we are trying to bind these elements with the index but this is not an easy task to do if we do not have the zipWithIndex method available we need to write so many lines of code to achieve this functionality. But in Scala, we have the zipWithIndex method available so we can call this method directly on the collection, as you can see on the next line we are calling zipWithIndex with the collection variable name. So once we print out the output we will see it will create an index for every element present in the list. For example, the sample output will look like this see below;

e.g. :

(hello, 0),(bye, 1),(catch, 2),(dog, 3),(ele, 4),(fan, 5,(pink, 6)

In the above output as you can see it has created an extra variable with the element of the list. So in this manner, it works in Scala by the use of this we can directly access the list element without being them iterate. It will return us the new tuple without modified the existing collection in Scala.

points to remember while working with zipwithindex method in Scala;

This method is used to create the index for the element.

This method can be used with both types of collection data structure i.e. immutable and mutable.

As the name suggests it will zip all the elements of the list with the newly created index with it, and returns a new collection for use.

Examples

1) In this example we are creating different types of lists in Scala and after this, we are calling zipWithIndex method to create the index for the list element, this will be a new list return by the zipWithIndex method in Scala. It contains list of String, integer, and so on for beginners.

Example #1

object Demo {
def main(args: Array[String]) {
println("demo to show zipwithindex method in Scala !!!")
val list1 = List("10", "20", "30", "40", "50", "60", "70")
val list2 = List("hello", "bye", "catch", "home", "world", "see you ")
val list3 = List(100, 200, 300, 400, 500, 00, 700, 800, 900)
//printing them for output
println("values inside the list 1 before zipwithindex method ::")
println(list1)
println("values inside the list 2 before zipwithindex method ::")
println(list2)
println("values inside the list 3 before zipwithindex method ::")
println(list3)
println("***********************************************************************************")
// calling zipWithIndex method to create new list and index for each element
val zip1 = list1.zipWithIndex
val zip2 = list2.zipWithIndex
val zip3 = list3.zipWithIndex
// printing the result after zipWithIndex method called.
println("values inside the list 1 after zipwithindex method ::")
println(zip1)
println("values inside the list 2 after zipwithindex method ::")
println(zip2)
println("values inside the list 3 aftre zipwithindex method ::")
println(zip3)
}
}


No comments:

Post a Comment